package com.micai.springboot;

import com.micai.springboot.filter.SLogFilter;
import org.activiti.JsonpCallbackFilter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.Filter;

/**
 * 描述：
 * <p>
 * Author: 赵新国
 * Date: 2017/10/24 18:26
 */
// Spring Boot 应用的标识
@SpringBootApplication
@ComponentScan(basePackages = {"com.micai.springboot","org.activiti"})
@MapperScan("com.micai.springboot.mapper")// mapper 接口类扫描包配置
@EnableTransactionManagement
@EnableSwagger2
@EnableCaching
@EnableAsync // 开启异步调用支持
//@EnableRedisHttpSession // 开启Spring Session支持
//@EnableAutoConfiguration(exclude = {SecurityAutoConfiguration.class}) // 解决弹出登录框的问题
public class Application {

    @Bean
    public Filter sLogFilter(){
        return new SLogFilter();//自定义的过滤器
    }

    @Bean
    public Filter jsonpCallbackFilter(){
        return new JsonpCallbackFilter();//自定义的过滤器
    }

    /**
     * 注册自定义Filter
     * @return
     */
    @Bean
    public FilterRegistrationBean indexFilterRegistration() {
        // 注册记录日志Filter
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(sLogFilter());
        registration.addUrlPatterns("/admin/*");
        return registration;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean2(){
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(jsonpCallbackFilter());
        filterRegistrationBean.addUrlPatterns("/service/*");
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }

    public static void main(String[] args) {
        // 程序启动入口
        // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
        SpringApplication.run(Application.class,args);
    }
}
